import { loginByUsername, getUserInfo, userMenu } from '@/utils/api'
import { getToken, setToken, removeToken, removeSite } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    menu: null,
    userId: 0
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_USERID: (state, id) => {
      state.userId = id
    },
    SET_MENU: (state, menu) => {
      state.menu = menu
    }
  },

  actions: {
    // 获取用户菜单
    async GetUserMenu({ commit, state }) {
      return new Promise(async(resolve, reject) => {
        const data = await userMenu()
        commit('SET_MENU', data)
        resolve(data)
      })
    },

    // 用户名登录
    async LoginByUsername({ commit }, userInfo) {
      return new Promise(async(resolve, reject) => {
        try {
          const data = await loginByUsername(userInfo)
          removeSite()
          setToken(data.accessToken.value)
          commit('SET_TOKEN', data.accessToken.value)
          resolve(data)
        } catch (e) {
          reject()
        }
      })
    },

    // 获取用户信息
    async GetUserInfo({ commit, state }) {
      return new Promise(async(resolve, reject) => {
        try {
          const data = await getUserInfo()
          commit('SET_NAME', data.userName)
          commit('SET_USERID', data.userId)
          resolve(data)
        } catch (e) {
          reject()
        }
      })

      //    return new Promise((resolve, reject) => {
      //      getUserInfo(state.token).then(response => {
      //        if (!response.data) { // 由于mockjs 不支持自定义状态码只能这样hack
      //          reject('error')
      //        }
      //        const data = response.data
      //
      //        if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
      //          commit('SET_ROLES', data.roles)
      //        } else {
      //          reject('getInfo: roles must be a non-null array !')
      //        }
      //
      //        commit('SET_NAME', data.name)
      //        commit('SET_AVATAR', data.avatar)
      //        commit('SET_INTRODUCTION', data.introduction)
      //        resolve(response)
      //      }).catch(error => {
      //        reject(error)
      //      })
      //    })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        removeSite()
        removeToken()
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({ commit }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          resolve()
        })
      })
    }
  }
}

export default user
